package calendar;

import calendar.messages.*;
import edu.gmu.messages.EndPoint;
import edu.gmu.middleware.RemoteStub;
import edu.gmu.middleware.Message;
import edu.gmu.server.RmiSkeleton;
import java.io.IOException;
import java.util.ArrayList;

public class CalendarMgrSkeleton extends RmiSkeleton
{
    private CalendarMgr calendarMgr = null;
    private EndPoint endpoint       = null;
    
    /**
     * Creates a new CalendarSkeleton with specified params.
     */
    public CalendarMgrSkeleton( CalendarMgr calendarMgr,
                                EndPoint endpoint,
                                String name )
    {
        super( endpoint.port, endpoint.port, name );
        this.endpoint    = endpoint;
        this.calendarMgr = calendarMgr;
    }
    
    public EndPoint getEndPoint()
    {
        return this.endpoint;
    }
    
    public CalendarMgr getCalendarMgr()
    {
        return this.calendarMgr;
    }
    
    
    /**
     * Handles a new client connection.
     * @param Socket clientSocket
     */
    public void handleRequests( RemoteStub stub ) throws IOException
    {
        //--------------------------------------------------------------------//
        // Executed in handlerThread
        //--------------------------------------------------------------------//
        stub.register(Topic.CREATE_CALENDAR_REQUEST, User.class);
        stub.register(Topic.LIST_REQUEST,            ListRequest.class);
        stub.register(Topic.CONNECT_REQUEST,         ConnectRequest.class);
        stub.register(Topic.DISCONNECT_REQUEST,      DisconnectRequest.class);
        stub.register(Topic.ALIVE_REQUEST,           AliveRequest.class);
            
        /*
         * Read from clientsocket continusouyly until error or disconnect 
         */
        //boolean disconnect = false;
        while( stopped == false )
        {
            Message message = stub.receive();
            
            if( message instanceof User )
            {
                User request = (User)message;
                
                boolean aResult = this.calendarMgr.createCalendar(request);
                
                Result result = new Result();
                result.result = aResult;
                
                stub.send(Topic.CREATE_CALENDAR_RESPONSE, result);
            }
            else if( message instanceof ListRequest )
            {
                ListRequest request = (ListRequest)message;
                
                ArrayList users = this.calendarMgr.list();
                
                ListResponse response = new ListResponse();
                response.users = users;
                
                stub.send(Topic.LIST_RESPONSE, response); 
            }
            else if( message instanceof ConnectRequest )
            {
                ConnectRequest request = (ConnectRequest)message;
                
                boolean result = this.calendarMgr.connectCalendar(request.user);
                
                ConnectResponse response = new ConnectResponse();
                response.result = result;
                
                stub.send(Topic.CONNECT_RESPONSE, response); 
            }    
            else if( message instanceof DisconnectRequest )
            {
                DisconnectRequest request = (DisconnectRequest)message;
                
                boolean result = this.calendarMgr.disconnectCalendar(request.user);
                
                DisconnectResponse response = new DisconnectResponse();
                response.result = result;
                
                stub.send(Topic.DISCONNECT_RESPONSE, response); 
            } 
            else if( message instanceof AliveRequest )
            {
                AliveRequest request = (AliveRequest)message;
                
                this.calendarMgr.isAlive();
                
                AliveResponse response = new AliveResponse();
                
                stub.send(Topic.ALIVE_RESPONSE, response);
            }
        }
        stub.stop();
        //--------------------------------------------------------------------//
    }
    
    

}
